﻿using Sustainalytics.Utils.Configuration;
using Svg;
using System;
using System.Drawing;
using System.IO;
using Sustainalytics.AwsS3.Client;

namespace Sustainalytics.ReportGeneratorFramework
{
    public static class Utils
    {
        private static readonly string _bucketName;
        private static readonly string _prefix;

        static Utils()
        {
            _bucketName = SettingsHelper.Read("s3-product-involvement-images", "product-involvement-images");
            _prefix = SettingsHelper.Read("s3prefix-product-involvement-images", "ProductInvolvement");
        }

        public static Foxit.PDF.Font GetSystemFont(string fontName)
        {
            Foxit.PDF.Font font;
            
            font = Foxit.PDF.Font.TimesRoman;

            try
            {
                if (Foxit.PDF.Font.CanLoadSystemFont(fontName))
                {
                    font = Foxit.PDF.Font.LoadSystemFont(fontName);
                }
                else
                {
                    throw new Exception(string.Format("System Font {0} not found", fontName));
                }
            }
            catch {
                //supress exception
            }

            return font;
        }

        public static Stream LoadS3IndicatorImage(string fileName)
        {
            fileName = $"{_prefix}/{fileName}";
            Stream imageByteArray = new System.IO.MemoryStream();

            S3ClientSettings _s3ClientSettings;

            _s3ClientSettings = new S3ClientSettings
            {
                AccessKey = SettingsHelper.Read("accesKeyId", "AKIAJKGZZMOFAVOVLHJA"),
                SecretAccessKey = SettingsHelper.Read("awsSecretKey", "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"),
                ServiceEndPoint = SettingsHelper.Read("s3ServiceEndPoint", "https://s3-eu-west-1.amazonaws.com/")
            };

            using (var s3Client = new S3Client(_s3ClientSettings))
            {
                try
                {
                    imageByteArray = s3Client.GetFileStream(_bucketName, fileName);

                }
                catch (Exception) { }
            }

            return imageByteArray;
        }

        public static byte[] ImageToByteArray(Image image)
        {
            var memoryStream = new MemoryStream();
            image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            return memoryStream.ToArray();
        }

        public static Image ByteArrayToImage(byte[] buffer)
        {
            var memoryStream = new MemoryStream(buffer);
            var image = Image.FromStream(memoryStream);
            return image;
        }

        public static byte[] GetPngImage(string filePath)
        {
            using (var fileStream = File.OpenRead(filePath))
            using (var image = Image.FromStream(fileStream))
            {
                var memoryStream = new MemoryStream();
                image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                return memoryStream.ToArray();
            }
        }

        public static byte[] GetPngFromSvgImage(string fileName)
        {
            var svgDocument = SvgDocument.Open<SvgDocument>(LoadS3IndicatorImage(fileName));
            svgDocument.Color = new SvgColourServer(System.Drawing.Color.Coral);
            System.Drawing.Bitmap bmp = svgDocument.Draw(100, 100);
            var memoryStream = new MemoryStream();
            bmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            return memoryStream.ToArray();
        }

        public static byte[] GetJpgImage(string filePath)
        {
            using (var fileStream = File.OpenRead(filePath))
            using (var image = Image.FromStream(fileStream))
            {
                var memoryStream = new MemoryStream();
                image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                return memoryStream.ToArray();
            }
        }

        public static void SaveImage(string filePath, byte[] picture)
        {
            using (var fileStream = File.OpenWrite(filePath))
            {
                fileStream.Write(picture, 0, picture.Length);
                fileStream.Close();
            }
        }


        public static string OrdinalNumberSuffix(int number)
        {
            string suffix = string.Empty;

            if (number < 1)
                return suffix;

            int ones = number % 10;
            int tens = (int)Math.Floor(number / 10f) % 10;

            if (tens == 1)
            {
                suffix = "th";
            }
            else
            {
                switch (ones)
                {
                    case 1:
                        suffix = "st";
                        break;

                    case 2:
                        suffix = "nd";
                        break;

                    case 3:
                        suffix = "rd";
                        break;

                    default:
                        suffix = "th";
                        break;
                }
            }

            return suffix;
        }

    }
}
